package org.jeecg.modules.controller.shippingAddress;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.shippingAddress.entity.City;
import org.jeecg.common.modules.shippingAddress.entity.Provincial;
import org.jeecg.common.modules.shippingAddress.entity.ShippingAddress;
import org.jeecg.common.modules.shippingAddress.service.CityService;
import org.jeecg.common.modules.shippingAddress.service.IShippingAddressService;
import org.jeecg.common.modules.shippingAddress.service.ProvincialService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 收货地址表
* @Author: jeecg-boot
* @Date:   2022-02-28
* @Version: V1.0
*/
@Api(tags="收货地址表")
@RestController
@RequestMapping("api/v1/shippingAddress")
@Slf4j
public class ShippingAddressController extends JeecgController<ShippingAddress, IShippingAddressService> {
   @Autowired
   private IShippingAddressService shippingAddressService;
   @Autowired
   private ProvincialService provincialService;
   @Autowired
   private CityService cityService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @AutoLog(value = "收货地址表-分页列表查询")
    @ApiOperation(value="收货地址表-分页列表查询", notes="收货地址表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name="openid",required=true) String openid) {
        List<ShippingAddress> shippingAddressList = shippingAddressService.list(Wrappers.lambdaQuery(ShippingAddress.class).eq(ShippingAddress::getOpenid,openid));
        return Result.ok(shippingAddressList);
    }

   /**
    *   添加
    *
    * @param shippingAddress
    * @return
    */
   @AutoLog(value = "收货地址表-添加")
   @ApiOperation(value="收货地址表-添加", notes="收货地址表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody ShippingAddress shippingAddress) {
       checkIsdefault(shippingAddress);
       shippingAddressService.save(shippingAddress);
       return Result.ok("添加成功！");
   }

   /**
    *  编辑
    *
    * @param shippingAddress
    * @return
    */
   @AutoLog(value = "收货地址表-编辑")
   @ApiOperation(value="收货地址表-编辑", notes="收货地址表-编辑")
   @PostMapping(value = "/edit")
   public Result<?> edit(@RequestBody ShippingAddress shippingAddress) {
       checkIsdefault(shippingAddress);
       shippingAddressService.updateById(shippingAddress);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "收货地址表-通过id删除")
   @ApiOperation(value="收货地址表-通过id删除", notes="收货地址表-通过id删除")
   @PostMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       shippingAddressService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "收货地址表-批量删除")
   @ApiOperation(value="收货地址表-批量删除", notes="收货地址表-批量删除")
   @PostMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.shippingAddressService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "收货地址表-通过id查询")
   @ApiOperation(value="收货地址表-通过id查询", notes="收货地址表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       ShippingAddress shippingAddress = shippingAddressService.getById(id);
       if(shippingAddress==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(shippingAddress);
   }
    /**
     *   获取省份列表
     *
     * @param provincialVO
     * @return
     */
    @AutoLog(value = "获取省份列表")
    @ApiOperation(value="获取省份列表", notes="获取省份列表")
    @PostMapping(value = "/queryProvice")
    public Result<?> queryProvice(@RequestBody ProvincialVO provincialVO) {

        List<ProvincialVO> provincials = new ArrayList<>();
        if(provincialVO == null||provincialVO.getPid() == null){
            LambdaQueryWrapper<Provincial> query = Wrappers.lambdaQuery();
            List<Provincial> provincialList =  provincialService.list(query);
            if(provincialList!=null){
                for (Provincial provincial1:provincialList) {
                    ProvincialVO provincial = new ProvincialVO();
                    BeanUtils.copyProperties(provincial1,provincial);
                    provincials.add(provincial);
                }
            }
        }else {
            LambdaQueryWrapper<City> query = Wrappers.lambdaQuery();
            query.eq(City::getPid,provincialVO.getPid());
            List<City> cityList =  cityService.list(query);
            if(cityList!=null){
                for (City city:cityList){
                    ProvincialVO provincial = new ProvincialVO();
                    BeanUtils.copyProperties(city,provincial);
                    provincials.add(provincial);
                }
            }
        }
        return Result.ok(provincials);
    }

    private void checkIsdefault(ShippingAddress shippingAddress){
        List<ShippingAddress> shippingAddressList = new ArrayList<>();
        if(shippingAddress!=null || "Y".equals(shippingAddress.getIsDefault()) ){
            shippingAddressList = shippingAddressService.list(Wrappers.lambdaQuery(ShippingAddress.class).eq(ShippingAddress::getOpenid,shippingAddress.getOpenid()));
            for (ShippingAddress s:  shippingAddressList) {
                s.setIsDefault("N");
                shippingAddressService.updateById(s);
            }
        }
    }

}
